package com.zuimei.zuimei.activities;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.adapters.MeiYouHomeAdapter;
import com.zuimei.zuimei.base.MySwipeBackActivity;
import com.zuimei.zuimei.beans.CollectionsBean;
import com.zuimei.zuimei.beans.FindAppItem2Bean;
import com.zuimei.zuimei.beans.FindAppItem5Bean;
import com.zuimei.zuimei.http.HttpHelper;
import com.zuimei.zuimei.jsonutil.ParseFindAppJson;
import com.zuimei.zuimei.utils.ConstantUtil;
import com.zuimei.zuimei.widgets.CircleImageView;

import java.util.List;

import butterknife.Bind;

/**
 * 美友主页
 */

public class MeiYouHomeActivity extends MySwipeBackActivity implements View.OnClickListener {

    @Bind(R.id.fl_container)
    FrameLayout fl_container;
    @Bind(R.id.ibtn_back)
    ImageButton ibtn_back;
    @Bind(R.id.iv_avatar)
    CircleImageView iv_avatar;
    @Bind(R.id.tv_user_name)
    TextView tv_user_name;
    @Bind(R.id.tv_career)
    TextView tv_career;
    @Bind(R.id.ll_layout)
    LinearLayout ll_layout;
    @Bind(R.id.rl_head_container)
    RelativeLayout rl_head_container;
    @Bind(R.id.tv_recommand_count)
    TextView tv_recommand_count;
    @Bind(R.id.tv_collect_count)
    TextView tv_collect_count;
    @Bind(R.id.ll_head_container)
    LinearLayout ll_head_container;
    @Bind(R.id.ll_recommand)
    LinearLayout ll_recommand;
    @Bind(R.id.ll_collections)
    LinearLayout ll_collections;
    @Bind(R.id.vp_content)
    ViewPager vp_content;
    @Bind(R.id.tl_title)
    TabLayout tl_title;
    private List<FindAppItem2Bean> mRecommandAppList;
    private List<CollectionsBean> mCollectionsList;
    private FindAppItem5Bean.UserBean mUser;
    private MeiYouHomeAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mei_you_home;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    }

    protected void init() {
        ibtn_back.setOnClickListener(this);
        ll_recommand.setOnClickListener(this);
        ll_collections.setOnClickListener(this);
        tl_title.setTabMode(TabLayout.MODE_FIXED);
        mUser = (FindAppItem5Bean.UserBean) getIntent().getSerializableExtra("user");
        fl_container.setBackgroundColor(Color.parseColor(mUser.getBg_color()));
        Picasso.with(this)
                .load(mUser.getAvatar_url())
                .config(Bitmap.Config.RGB_565)
                .into(iv_avatar);
        tv_user_name.setText(mUser.getName());
        tv_career.setText(mUser.getCareer());
    }

    protected void loadData() {
        ll_head_container.measure(0, 0);
        rl_head_container.measure(0, 0);
        //加载推荐数据
        //final int rlHeadHeight = rl_head_container.getMeasuredHeight();
        new HttpHelper(this).getData(String.format(ConstantUtil.USER_RECOMMAND_URL, mUser.getId()
                , 1), new HttpHelper.OnLoadCompletedListener() {


            @Override
            public void onSuccess(String result) {
                mRecommandAppList = ParseFindAppJson.getRecommandAppList
                        (result);
                tv_recommand_count.setText(mRecommandAppList.size() + "");

                //加载收藏数据
                new HttpHelper(getBaseContext()).getData(String.format(ConstantUtil
                        .USER_COLLECT_URL, mUser.getId(), 1), new HttpHelper
                        .OnLoadCompletedListener() {


                    @Override
                    public void onSuccess(String result) {
                        mCollectionsList = ParseFindAppJson.getCollectionsList(result);
                        mAdapter = new MeiYouHomeAdapter(getSupportFragmentManager(), new
                                String[]{"", ""}, mRecommandAppList, mCollectionsList);
                        vp_content.setAdapter(mAdapter);
                        tl_title.setupWithViewPager(vp_content);
                    }

                    @Override
                    public void onFailure() {

                    }
                });

            }

            @Override
            public void onFailure() {

            }
        });

//        rv_content.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            boolean isOver = true;
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                //dy:上滑为正，下滑为负
//
//                Log.d("TAG", "--->" + rlHeadHeight + "---" + ll_head_container
//                        .getMeasuredHeight() + "---" + dy);
//                // ll_head_container.setY(rlHeadHeight);
//                int position = mLayoutManager.findFirstVisibleItemPosition();
//                View view = mLayoutManager.findViewByPosition(position);
//                Log.d("TAG", "--:" + view.getY());
//                if (position == 0) {
//                    if (dy > 0 && view.getY() > -50) {
//                        if (isOver){
//                            ll_layout.animate().translationY(0).y(-rlHeadHeight+ ScreenUtil
// .getStatusHeight(getBaseContext()))
//                                    .setDuration(500).start();
//                                    rv_content.layout(0,ll_head_container.getHeight()+
// ScreenUtil.getStatusHeight(getBaseContext()),rv_content.getRight(),rlHeadHeight+rv_content
// .getBottom());
//                               isOver = false;
//                        }
//
//                    } else if (dy < 0 && view.getY() == 0) {
//                        if (!isOver) {
//                            ll_layout.animate().translationY(-rlHeadHeight+ ScreenUtil
// .getStatusHeight(getBaseContext())).y(0)
//                                    .setDuration(500).start();
//
//                            rv_content.layout(0,ll_layout.getHeight()+ ScreenUtil
// .getStatusHeight(getBaseContext()),rv_content.getRight(),rv_content.getBottom());
//                            isOver = true;
//                        }
//                    }
//                }
//            }
//        });
    }

    //@OnClick({R.id.ibtn_back, R.id.tv_recommand_count, R.id.tv_collect_count,R.id
    // .ll_head_container})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ibtn_back:
                finish();
                break;
            case R.id.ll_recommand:
                vp_content.setCurrentItem(0);
                break;

            case R.id.ll_collections:
                vp_content.setCurrentItem(1);
                break;
        }
    }
}
